<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Git操作清单 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Git,">
  

  <meta name="description" content="一、必备知识点   Remote: 远程主仓库 Repository/History： 本地仓库 Stage/Index： Git追踪树,暂存区 workspace： 本地工作区（即你编辑器的代码）  二、git add 提交到暂存区，出错怎么办 一般代码提交流程为：工作区 -&amp;gt; git status 查看状态 -&amp;gt; git add . 将所有修改加入暂存区-&amp;gt; git comm">
<meta name="keywords" content="Git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git操作清单">
<meta property="og:url" content="http://blog.poetries.top/2019/08/31/git-review/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、必备知识点   Remote: 远程主仓库 Repository/History： 本地仓库 Stage/Index： Git追踪树,暂存区 workspace： 本地工作区（即你编辑器的代码）  二、git add 提交到暂存区，出错怎么办 一般代码提交流程为：工作区 -&amp;gt; git status 查看状态 -&amp;gt; git add . 将所有修改加入暂存区-&amp;gt; git comm">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1480597-e53fe40cce1f8eeb.png">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1480597-d907599b730fdeb0.png">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1480597-140cfe6932a62a57.png">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1480597-6d83bae1d4f45a86.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.910Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git操作清单">
<meta name="twitter:description" content="一、必备知识点   Remote: 远程主仓库 Repository/History： 本地仓库 Stage/Index： Git追踪树,暂存区 workspace： 本地工作区（即你编辑器的代码）  二、git add 提交到暂存区，出错怎么办 一般代码提交流程为：工作区 -&amp;gt; git status 查看状态 -&amp;gt; git add . 将所有修改加入暂存区-&amp;gt; git comm">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/1480597-e53fe40cce1f8eeb.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、必备知识点"><span class="toc-text">一、必备知识点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、git-add-提交到暂存区，出错怎么办"><span class="toc-text">二、git add 提交到暂存区，出错怎么办</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、git-commit-提交到本地仓库，出错怎么办？"><span class="toc-text">三、git commit 提交到本地仓库，出错怎么办？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、常用命令"><span class="toc-text">四、常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-初始开发-git-操作流程"><span class="toc-text">4.1 初始开发 git 操作流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-git-fetch"><span class="toc-text">4.2 git fetch</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-git-pull"><span class="toc-text">4.3 git pull</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-git-push"><span class="toc-text">4.4 git push</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-分支操作"><span class="toc-text">4.5 分支操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、优化操作"><span class="toc-text">五、优化操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-拉取代码-pull-–rebase"><span class="toc-text">5.1 拉取代码 pull –rebase</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-合代码-merge-–no-ff"><span class="toc-text">5.2 合代码 merge –no-ff</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、SSH"><span class="toc-text">六、SSH</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#七、暂存"><span class="toc-text">七、暂存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#八、文件名过长错误"><span class="toc-text">八、文件名过长错误</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#九、邮箱和用户名"><span class="toc-text">九、邮箱和用户名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十、-gitignore-更新后生效："><span class="toc-text">十、.gitignore 更新后生效：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#十一、同步Github-fork-出来的分支"><span class="toc-text">十一、同步Github fork 出来的分支</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-git-review" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Git操作清单</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2019.08.31</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/VCS/">VCS</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 3.1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 12分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、必备知识点"><a href="#一、必备知识点" class="headerlink" title="一、必备知识点"></a>一、必备知识点</h2><p><img src="https://upload-images.jianshu.io/upload_images/1480597-e53fe40cce1f8eeb.png" alt></p>
<p><img src="https://upload-images.jianshu.io/upload_images/1480597-d907599b730fdeb0.png" alt></p>
<ul>
<li><code>Remote</code>: 远程主仓库</li>
<li><code>Repository/History</code>： 本地仓库</li>
<li><code>Stage/Index</code>： Git追踪树,暂存区</li>
<li><code>workspace</code>： 本地工作区（即你编辑器的代码）</li>
</ul>
<h2 id="二、git-add-提交到暂存区，出错怎么办"><a href="#二、git-add-提交到暂存区，出错怎么办" class="headerlink" title="二、git add 提交到暂存区，出错怎么办"></a>二、git add 提交到暂存区，出错怎么办</h2><blockquote>
<p>一般代码提交流程为：工作区 -&gt; <code>git status</code> 查看状态 -&gt; <code>git add .</code> 将所有修改加入暂存区-&gt; <code>git commit -m &quot;提交描述&quot;</code> 将代码提交到 本地仓库 -&gt; <code>git push</code> 将本地仓库代码更新到 远程仓库</p>
</blockquote>
<p><strong>场景1：</strong></p>
<blockquote>
<p>当你改乱了工作区某个文件的内容，想直接丢弃工作区的修改时，用命令 <code>git checkout -- file</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 丢弃工作区的修改</span></span><br><span class="line">git checkout -- &lt;文件名&gt;</span><br></pre></td></tr></table></figure>
<p><strong>场景2：</strong></p>
<blockquote>
<p>当你不但改乱了工作区某个文件的内容，还添加到了暂存时，想丢弃修改，分两步，第一步用命令 <code>git reset HEAD file</code>，就回到了场景1，第二步按场景1操作</p>
</blockquote>
<h2 id="三、git-commit-提交到本地仓库，出错怎么办？"><a href="#三、git-commit-提交到本地仓库，出错怎么办？" class="headerlink" title="三、git commit 提交到本地仓库，出错怎么办？"></a>三、git commit 提交到本地仓库，出错怎么办？</h2><p><strong>1.  提交信息出错</strong></p>
<blockquote>
<p>更改 commit 信息</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">git commit --amend -m“新提交消息”</span><br></pre></td></tr></table></figure>
<p><strong>2. 漏提交</strong></p>
<blockquote>
<p><code>commit</code> 时，遗漏提交部分更新，有两种解决方案：</p>
</blockquote>
<ol>
<li>方案一：再次 <code>commit</code></li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">git commit -m“提交消息”</span><br></pre></td></tr></table></figure>
<blockquote>
<p>此时，<code>git</code> 上会出现两次 <code>commit</code></p>
</blockquote>
<ol start="2">
<li>方案二：遗漏文件提交到之前 <code>commit</code> 上</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">git add missed-file <span class="comment">// missed-file 为遗漏提交文件</span></span><br><span class="line">git commit --amend --no-edit</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>--no-edit</code> 表示提交消息不会更改，在 <code>git</code> 上仅为一次提交</p>
</blockquote>
<p><strong>3. 提交错误文件，回退到上一个 commit 版本，再 commit</strong></p>
<ol>
<li><code>git reset</code></li>
</ol>
<blockquote>
<p>删除指定的 <code>commit</code></p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 修改版本库，修改暂存区，修改工作区</span></span><br><span class="line"></span><br><span class="line">git reset HEAD &lt;文件名&gt; <span class="comment">// 把暂存区的修改撤销掉（unstage），重新放回工作区。</span></span><br><span class="line"><span class="comment">// git版本回退，回退到特定的commit_id版本，可以通过git log查看提交历史，以便确定要回退到哪个版本(commit 之后的即为ID);</span></span><br><span class="line">git reset --hard commit_id </span><br><span class="line"><span class="comment">//将版本库回退1个版本，不仅仅是将本地版本库的头指针全部重置到指定版本，也会重置暂存区，并且会将工作区代码也回退到这个版本</span></span><br><span class="line">git reset --hard HEAD~<span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 修改版本库，保留暂存区，保留工作区</span></span><br><span class="line"><span class="comment">// 将版本库软回退1个版本，软回退表示将本地版本库的头指针全部重置到指定版本，且将这次提交之后的所有变更都移动到暂存区。</span></span><br><span class="line">git reset --soft HEAD~<span class="number">1</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li><code>git revert</code></li>
</ol>
<blockquote>
<p>撤销某次操作，此次操作之前和之后的<code>commit</code>和<code>history</code>都会保留，并且把这次撤销</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 撤销前一次 commit</span></span><br><span class="line">git revert HEAD</span><br><span class="line"><span class="comment">// 撤销前前一次 commit</span></span><br><span class="line">git revert HEAD^</span><br><span class="line"><span class="comment">// (比如：fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff）撤销指定的版本，撤销也会作为一次提交进行保存。</span></span><br><span class="line">git revert commit</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>git revert</code> 是提交一个新的版本，将需要<code>revert</code>的版本的内容再反向修改回去，版本会递增，不影响之前提交的内容</p>
</blockquote>
<p><strong>git revert 和 git reset 的区别</strong></p>
<ul>
<li><code>git revert</code>是用一次新的<code>commit</code>来回滚之前的<code>commit</code>，<code>git reset</code>是直接删除指定的<code>commit</code></li>
<li>在回滚这一操作上看，效果差不多。但是在日后继续merge以前的老版本时有区别。因为<code>git revert</code>是用一次逆向的commit“中和”之前的提交，因此日后合并老的<code>branch</code>时，导致这部分改变不会再次出现，但是<code>git reset</code>是之间把某些<code>commit</code>在某个<code>branch</code>上删除，因而和老的<code>branch</code>再次<code>merge</code>时，这些被回滚的<code>commit</code>应该还会被引入</li>
<li><code>git reset</code> 是把HEAD向后移动了一下，而<code>git revert</code>是HEAD继续前进，只是新的<code>commit</code>的内容和要<code>revert</code>的内容正好相反，能够抵消要被<code>revert</code>的内容</li>
</ul>
<h2 id="四、常用命令"><a href="#四、常用命令" class="headerlink" title="四、常用命令"></a>四、常用命令</h2><h3 id="4-1-初始开发-git-操作流程"><a href="#4-1-初始开发-git-操作流程" class="headerlink" title="4.1 初始开发 git 操作流程"></a>4.1 初始开发 git 操作流程</h3><ul>
<li>克隆最新主分支项目代码 <code>git clone 地址</code></li>
<li>创建本地分支 <code>git branch 分支名</code></li>
<li>查看本地分支 <code>git branch</code></li>
<li>查看远程分支 <code>git branch -a</code></li>
<li>切换分支  <code>git checkout 分支名</code>(一般修改未提交则无法切换，大小写问题经常会有，可强制切换  <code>git checkout 分支名 -f</code>  非必须慎用)</li>
<li>将本地分支推送到远程分支 <code>git push &lt;远程仓库&gt; &lt;本地分支&gt;:&lt;远程分支&gt;</code></li>
</ul>
<h3 id="4-2-git-fetch"><a href="#4-2-git-fetch" class="headerlink" title="4.2 git fetch"></a>4.2 git fetch</h3><blockquote>
<p>将某个远程主机的更新，全部/分支 取回本地（此时之更新了Repository）它取回的代码对你本地的开发代码没有影响，如需彻底更新需合并或使用<code>git pull</code></p>
</blockquote>
<h3 id="4-3-git-pull"><a href="#4-3-git-pull" class="headerlink" title="4.3 git pull"></a>4.3 git pull</h3><blockquote>
<p>拉取远程主机某分支的更新，再与本地的指定分支合并（相当与<code>fetch</code>加上了合并分支功能的操作）</p>
</blockquote>
<h3 id="4-4-git-push"><a href="#4-4-git-push" class="headerlink" title="4.4 git push"></a>4.4 git push</h3><blockquote>
<p>将本地分支的更新，推送到远程主机，其命令格式与<code>git pull</code>相似</p>
</blockquote>
<h3 id="4-5-分支操作"><a href="#4-5-分支操作" class="headerlink" title="4.5 分支操作"></a>4.5 分支操作</h3><ul>
<li>使用 <code>Git</code> 下载指定分支命令为：<code>git clone -b 分支名仓库地址</code></li>
<li>拉取远程新分支 <code>git checkout -b serverfix origin/serverfix</code></li>
<li>合并本地分支 <code>git merge hotfix</code>：(将 <code>hotfix</code> 分支合并到当前分支)</li>
<li>合并远程分支 <code>git merge origin/serverfix</code></li>
<li>删除本地分支 <code>git branch -d hotfix</code>：(删除本地 <code>hotfix</code> 分支)</li>
<li>删除远程分支 <code>git push origin --delete serverfix</code></li>
<li>上传新命名的本地分支：<code>git push origin newName;</code></li>
<li>创建新分支：<code>git branch branchName</code>：(创建名为 <code>branchName</code> 的本地分支)</li>
<li>切换到新分支：<code>git checkout branchName</code>：(切换到 <code>branchName</code> 分支)</li>
<li>创建并切换分支：<code>git checkout -b branchName</code>：(相当于以上两条命令的合并)</li>
<li>查看本地分支：<code>git branch</code></li>
<li>查看远程仓库所有分支：<code>git branch -a</code></li>
<li>本地分支重命名：<code>git branch -m oldName newName</code></li>
<li>重命名远程分支对应的本地分支：<code>git branch -m oldName newName</code></li>
<li>把修改后的本地分支与远程分支关联：<code>git branch --set-upstream-to origin/newName</code></li>
</ul>
<h2 id="五、优化操作"><a href="#五、优化操作" class="headerlink" title="五、优化操作"></a>五、优化操作</h2><h3 id="5-1-拉取代码-pull-–rebase"><a href="#5-1-拉取代码-pull-–rebase" class="headerlink" title="5.1 拉取代码 pull –rebase"></a>5.1 拉取代码 pull –rebase</h3><ul>
<li>在团队协作过程中，假设你和你的同伴在本地中分别有各自的新提交，而你的同伴先于你 <code>push</code> 了代码到远程分支上，所以你必须先执行 <code>git pull</code> 来获取同伴的提交，然后才能<code>push</code> 自己的提交到远程分支。</li>
<li>而按照 <code>Git</code> 的默认策略，如果远程分支和本地分支之间的提交线图有分叉的话（即不是 fast-forwarded），<code>Git</code> 会执行一次 <code>merge</code> 操作，因此产生一次没意义的提交记录，从而造成了像上图那样的混乱。</li>
<li>其实在 <code>pull</code> 操作的时候，，使用 <code>git pull --rebase</code>选项即可很好地解决上述问题。加上 <code>--rebase</code> 参数的作用是，提交线图有分叉的话，<code>Git</code> 会 <code>rebase</code> 策略来代替默认的 <code>merge</code> 策略。</li>
</ul>
<p>假设提交线图在执行 <code>pull</code> 前是这样的</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">    A---B---C  remotes/origin/master</span><br><span class="line">    /</span><br><span class="line">D---E---F---G  master</span><br></pre></td></tr></table></figure>
<p> 如果是执行 git pull 后，提交线图会变成这样：</p>
 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">    A---B---C remotes/origin/master</span><br><span class="line">    /         \</span><br><span class="line">D---E---F---G---H master</span><br></pre></td></tr></table></figure>
<blockquote>
<p>结果多出了 H 这个没必要的提交记录。如果是执行 <code>git pull --rebase</code> 的话，提交线图就会变成这样</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">remotes/origin/master</span><br><span class="line">   |</span><br><span class="line">D---E---A---B---C---F&apos;---G&apos;  master</span><br></pre></td></tr></table></figure>
<blockquote>
<p>F G 两个提交通过 <code>rebase</code> 方式重新拼接在 C 之后，多余的分叉去掉了，目的达到。</p>
</blockquote>
<ul>
<li>大多数时候，使用 <code>git pull --rebase</code>是为了使提交线图更好看，从而方便 <code>code review</code></li>
<li>不过，如果你对使用 <code>git</code> 还不是十分熟练的话，我的建议是 <code>git pull --rebase</code>多练习几次之后再使用，因为 <code>rebase</code> 在 <code>git</code> 中，算得上是『危险行为』</li>
<li>另外，还需注意的是，使用 <code>git pull --rebase</code>比直接 <code>pull</code> 容易导致冲突的产生，如果预期冲突比较多的话，建议还是直接 <code>pull</code></li>
</ul>
<p><strong>注意：</strong></p>
<blockquote>
<ul>
<li><code>git pull = git fetch + git merge</code></li>
<li><code>git pull --rebase = git fetch + git rebase</code></li>
</ul>
</blockquote>
<h3 id="5-2-合代码-merge-–no-ff"><a href="#5-2-合代码-merge-–no-ff" class="headerlink" title="5.2 合代码 merge –no-ff"></a>5.2 合代码 merge –no-ff</h3><ul>
<li>上述的 <code>git pull --rebase</code> 策略目的是修整提交线图，使其形成一条直线，而即将要用到的 <code>git merge --no-ff &lt;branch-name&gt;</code>策略偏偏是反行其道，刻意地弄出提交线图分叉出来。</li>
<li>假设你在本地准备合并两个分支，而刚好这两个分支是 <code>fast-forwarded</code> 的，那么直接合并后你得到一个直线的提交线图，当然这样没什么坏处，但如果你想更清晰地告诉你同伴：这一系列的提交都是为了实现同一个目的，那么你可以刻意地将这次提交内容弄成一次提交线图分叉。</li>
<li>执行 <code>git merge --no-ff &lt;branch-name&gt;</code> 的结果大概会是这样的：</li>
</ul>
<p><img src="https://upload-images.jianshu.io/upload_images/1480597-140cfe6932a62a57.png" alt></p>
<p>中间的分叉线路图很清晰的显示这些提交都是为了实现 complete adjusting user domains and tags</p>
<blockquote>
<p>在合并分支之前（假设要在本地将 <code>feature</code> 分支合并到 <code>dev</code> 分支），会先检查 <code>feature</code> 分支是否『部分落后』于远程 <code>dev</code> 分支</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git checkout dev</span><br><span class="line">git pull # 更新 dev 分支</span><br><span class="line">git log feature..dev</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果没有输出任何提交信息的话，即表示 <code>feature</code> 对于 <code>dev</code> 分支是 <code>up-to-date</code> 的。如果有输出的话而马上执行了 <code>git merge --no-ff</code> 的话，提交线图会变成这样</p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/1480597-6d83bae1d4f45a86.png" alt></p>
<p>所以这时在合并前，通常我会先执行</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git checkout feature</span><br><span class="line">git rebase dev</span><br></pre></td></tr></table></figure>
<ul>
<li>这样就可以将 <code>feature</code> 重新拼接到更新了的 <code>dev</code> 之后，然后就可以合并了，最终得到一个干净舒服的提交线图。</li>
<li>再次提醒：像之前提到的，<code>rebase</code> 是『危险行为』，建议你足够熟悉 <code>git</code> 时才这么做，否则的话是得不偿失啊。</li>
</ul>
<p><strong>总结</strong></p>
<ul>
<li>使用 <code>git pull --rebase</code> 和 <code>git merge --no-ff</code> 其实和直接使用 <code>git pull git merge</code> 得到的代码应该是一样。</li>
<li>使用 <code>git pull --rebase</code> 主要是为是将提交约线图平坦化，而 <code>git merge --no-ff</code> 则是刻意制造分叉</li>
</ul>
<h2 id="六、SSH"><a href="#六、SSH" class="headerlink" title="六、SSH"></a>六、SSH</h2><p><strong>1. 查看是否生成了 SSH 公钥</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cd ~/.ssh</span><br><span class="line">$ ls</span><br><span class="line">id_rsa      id_rsa.pub      known_hosts</span><br></pre></td></tr></table></figure>
<p>其中 id_rsa 是私钥，id_rsa.pub 是公钥</p>
<p><strong>2. 如果没有那就开始生成，设置全局的user.name与user.email</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">git config --list <span class="comment">// 查看是否设置了user.name与user.email，没有的话，去设置</span></span><br><span class="line"><span class="comment">// 设置全局的user.name与user.email</span></span><br><span class="line">git config --global user.name <span class="string">"XX"</span></span><br><span class="line">git config --global user.email <span class="string">"XX"</span></span><br></pre></td></tr></table></figure>
<p><strong>3. 输入 ssh-keygen 即可（或<code>ssh-keygen -t rsa -C &quot;email&quot;</code>）</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">$ ssh-keygen</span><br><span class="line">Generating public/private rsa key pair.</span><br><span class="line">Enter file <span class="keyword">in</span> which to save the key (<span class="regexp">/Users/</span>schacon/.ssh/id_rsa):</span><br><span class="line">Enter passphrase (empty <span class="keyword">for</span> no passphrase):</span><br><span class="line">Enter same passphrase again:</span><br><span class="line">Your identification has been saved <span class="keyword">in</span> /Users/schacon/.ssh/id_rsa.</span><br><span class="line">Your public key has been saved <span class="keyword">in</span> /Users/schacon/.ssh/id_rsa.pub.</span><br><span class="line">The key fingerprint is:</span><br></pre></td></tr></table></figure>
<p><strong>4. 生成之后获取公钥内容，输入 cat ~/.ssh/id_rsa.pub 即可， 复制 ssh-rsa 一直到 .local这一整段内容</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cat ~/.ssh/id_rsa.pub</span><br><span class="line">ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU</span><br><span class="line">GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3</span><br><span class="line">Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA</span><br><span class="line">t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En</span><br><span class="line">mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx</span><br><span class="line">NrRFi9wrf+M7Q== schacon@agadorlaptop.local</span><br></pre></td></tr></table></figure>
<p><strong>5. 打开 GitLab 或者 GitHub，点击头像，找到设置页</strong></p>
<p><strong>6. 左侧找到 SSH keys 按钮并点击，输入刚刚复制的公钥即可</strong></p>
<h2 id="七、暂存"><a href="#七、暂存" class="headerlink" title="七、暂存"></a>七、暂存</h2><blockquote>
<p><code>git stash</code> 可用来暂存当前正在进行的工作，比如想 <code>pull</code> 最新代码又不想 <code>commit</code> ， 或者另为了修改一个紧急的 <code>bug</code> ，先 <code>stash</code>，使返回到自己上一个 <code>commit</code>,，改完 <code>bug</code> 之后再 <code>stash pop</code> , 继续原来的工作；</p>
</blockquote>
<ul>
<li>添加缓存栈：<code>git stash</code> ;</li>
<li>查看缓存栈：<code>git stash list</code> ;</li>
<li>推出缓存栈：<code>git stash pop</code> ;</li>
<li>取出特定缓存内容：<code>git stash apply stash@{1}</code> ;</li>
</ul>
<h2 id="八、文件名过长错误"><a href="#八、文件名过长错误" class="headerlink" title="八、文件名过长错误"></a>八、文件名过长错误</h2><p>Filename too long warning: Clone succeeded, but checkout failed.</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git config --system core.longpaths true</span><br></pre></td></tr></table></figure>
<h2 id="九、邮箱和用户名"><a href="#九、邮箱和用户名" class="headerlink" title="九、邮箱和用户名"></a>九、邮箱和用户名</h2><p><strong>查看</strong></p>
<ul>
<li><code>git config user.name</code></li>
<li><code>git config user.email</code></li>
</ul>
<p><strong>修改</strong></p>
<ul>
<li><code>git config --global user.name &quot;username&quot;</code></li>
<li><code>git config --global user.email &quot;email&quot;</code></li>
</ul>
<h2 id="十、-gitignore-更新后生效："><a href="#十、-gitignore-更新后生效：" class="headerlink" title="十、.gitignore 更新后生效："></a>十、.gitignore 更新后生效：</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git rm -r --cached .</span><br><span class="line">git add .</span><br><span class="line">git commit -m &quot;.gitignore is now working”</span><br></pre></td></tr></table></figure>
<h2 id="十一、同步Github-fork-出来的分支"><a href="#十一、同步Github-fork-出来的分支" class="headerlink" title="十一、同步Github fork 出来的分支"></a>十一、同步Github fork 出来的分支</h2><p><strong>1. 配置remote，指向原始仓库</strong></p>
<blockquote>
<p>git remote add upstream <a href="https://github.com/InterviewMap/InterviewMap.git" target="_blank" rel="noopener">https://github.com/InterviewMap/InterviewMap.git</a></p>
</blockquote>
<p><strong>2. 上游仓库获取到分支，及相关的提交信息，它们将被保存在本地的 upstream/master 分支</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">git fetch upstream</span><br><span class="line"><span class="comment"># remote: Counting objects: 75, done.</span></span><br><span class="line"><span class="comment"># remote: Compressing objects: 100% (53/53), done.</span></span><br><span class="line"><span class="comment"># remote: Total 62 (delta 27), reused 44 (delta 9)</span></span><br><span class="line"><span class="comment"># Unpacking objects: 100% (62/62), done.</span></span><br><span class="line"><span class="comment"># From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY</span></span><br><span class="line"><span class="comment"># * [new branch] master -&gt; upstream/master</span></span><br></pre></td></tr></table></figure>
<p><strong>3. 切换到本地的 master 分支</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">git checkout master</span><br><span class="line"><span class="comment"># Switched to branch 'master'</span></span><br></pre></td></tr></table></figure>
<p><strong>4. 把 upstream/master 分支合并到本地的 master 分支，本地的 master 分支便跟上游仓库保持同步了，并且没有丢失本地的修改</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">git merge upstream/master</span><br><span class="line"><span class="comment"># Updating a422352..5fdff0f</span></span><br><span class="line"><span class="comment"># Fast-forward</span></span><br><span class="line"><span class="comment"># README | 9 -------</span></span><br><span class="line"><span class="comment"># README.md | 7 ++++++</span></span><br><span class="line"><span class="comment"># 2 files changed, 7 insertions(+), 9 deletions(-)</span></span><br><span class="line"><span class="comment"># delete mode 100644 README</span></span><br><span class="line"><span class="comment"># create mode 100644 README.md</span></span><br></pre></td></tr></table></figure>
<p><strong>5. 上传到自己的远程仓库中</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">git push</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2019/08/31/taro-echarts/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2019/09/01/react-children/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
